// LGUICheckbox.cpp: implementation of the LGUICheckbox class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "LGUICheckbox.h"
#include "LGUIContainer.h"
#include "LAMEFont.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

LGUICheckbox::LGUICheckbox(TCHAR *_text, int id, int _x, int _y, int _w, int _h, bool _value, int _userdata) : LGUIWindow(_x,_y,_w,_h)
{
	pressed=lastPressed=down=false;
	value=_value;
	ID=id;
	text=_text;
	w=fonts[0].textSize(text) + h+4;
	setUserData(_userdata);
}

LGUICheckbox::~LGUICheckbox()
{

}



void LGUICheckbox::draw(int xoff, int yoff)
{
	if (isDirty)
	{
		buffer.rectFillFaded(x+xoff,y+yoff,x+xoff+h,y+yoff+h,pressed ? COLOR(0x808080) : COLOR(0xffffff),pressed?COLOR(0x606060):COLOR(0xcfcfcf));
		if (value)
			buffer.rectFillFaded(x+xoff+2,y+yoff+2,x+xoff+h-1,y+yoff+h-1,COLOR(0x006000),pressed?COLOR(0x80ff80):COLOR(0x00aC00));
		buffer.rect(x+xoff,y+yoff,x+xoff+h,y+yoff+h,pressed ? COLOR(0x80ff80) : COLOR(0x404040));
		buffer.drawText(text,x+xoff+h+4,y+yoff+(h-fonts[0].textHeight())/2+2,0);
		LRect dirty = *this;
		dirty.x += xoff;
		dirty.y += yoff;
		dirty.h++;
		buffer.addDirtyRect(dirty);

		isDirty=false;
	}
}


void LGUICheckbox::penDown(int px, int py)
{
	if (isInside(px,py))
	{
		lastPressed=false;
		pressed=true;
		down=true;
		redraw();
	}
}


void LGUICheckbox::penMove(int px, int py, bool p)
{
	if (!p || !down) return;
	pressed=isInside(px,py);
	if (lastPressed != pressed)
		redraw();
	lastPressed=pressed;
}

void LGUICheckbox::penUp(int px, int py)
{
	pressed=false;
	if (down) redraw();
	if (isInside(px,py) && down)
	{
		down=false;
		value=!value;
		if (parent)
			parent->deliverMessage(LGUIMessage(MSG_CHECKBOXCHECKED,ID,value,userData));
	}
}